<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/dxl/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/dxl/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/dxl/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/dxl/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/dxl/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/dxl/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/dxl/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/dxl/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/dxl/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="爬虫,">










<meta name="description" content="一、pandas简单介绍1、pandas是一个强大的Python数据分析的工具包 2、pandas是基于NumPy构建的 3、pandas的主要功能 1234具备对其功能的数据结构DataFrame、Series集成时间序列功能提供丰富的数学运算和操作灵活处理缺失数据  4、安装 1pip install pandas  5、引用方法 1import pandas as pd  二、Series​">
<meta name="keywords" content="爬虫">
<meta property="og:type" content="article">
<meta property="og:title" content="数据分析pandas">
<meta property="og:url" content="http://yoursite.com/2019/09/20/【数据分析02】数据分析二/index.html">
<meta property="og:site_name" content="我的快乐时光">
<meta property="og:description" content="一、pandas简单介绍1、pandas是一个强大的Python数据分析的工具包 2、pandas是基于NumPy构建的 3、pandas的主要功能 1234具备对其功能的数据结构DataFrame、Series集成时间序列功能提供丰富的数学运算和操作灵活处理缺失数据  4、安装 1pip install pandas  5、引用方法 1import pandas as pd  二、Series​">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-09-20T12:32:31.274Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据分析pandas">
<meta name="twitter:description" content="一、pandas简单介绍1、pandas是一个强大的Python数据分析的工具包 2、pandas是基于NumPy构建的 3、pandas的主要功能 1234具备对其功能的数据结构DataFrame、Series集成时间序列功能提供丰富的数学运算和操作灵活处理缺失数据  4、安装 1pip install pandas  5、引用方法 1import pandas as pd  二、Series​">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/dxl/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/09/20/【数据分析02】数据分析二/">





  <title>数据分析pandas | 我的快乐时光</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/dxl/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">我的快乐时光</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/dxl/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/dxl/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/dxl/2019/09/20/【数据分析02】数据分析二/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content>
      <meta itemprop="description" content>
      <meta itemprop="image" content="/dxl/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我的快乐时光">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">数据分析pandas</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-20T20:44:46+08:00">
                2019-09-20
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/dxl/categories/爬虫/" itemprop="url" rel="index">
                    <span itemprop="name">爬虫</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/dxl/2019/09/20/【数据分析02】数据分析二/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/dxl/2019/09/20/【数据分析02】数据分析二/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一、pandas简单介绍"><a href="#一、pandas简单介绍" class="headerlink" title="一、pandas简单介绍"></a>一、pandas简单介绍</h2><p>1、pandas是一个强大的Python数据分析的工具包</p>
<p>2、pandas是基于NumPy构建的</p>
<p>3、pandas的主要功能</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">具备对其功能的数据结构DataFrame、Series</span><br><span class="line">集成时间序列功能</span><br><span class="line">提供丰富的数学运算和操作</span><br><span class="line">灵活处理缺失数据</span><br></pre></td></tr></table></figure>

<p>4、安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install pandas</span><br></pre></td></tr></table></figure>

<p>5、引用方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import pandas as pd</span><br></pre></td></tr></table></figure>

<h2 id="二、Series"><a href="#二、Series" class="headerlink" title="二、Series"></a>二、Series</h2><p>​        Series是一种类似于一位数组的对象，由一组数据和一组与之相关的数据标签（索引）组成。</p>
<h3 id="2-1创建Series"><a href="#2-1创建Series" class="headerlink" title="2.1创建Series"></a>2.1创建Series</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="number">1</span>、列表形式</span><br><span class="line">pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>])</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string"> 	0    1</span></span><br><span class="line"><span class="string">    1    2</span></span><br><span class="line"><span class="string">    2    3</span></span><br><span class="line"><span class="string">    3    4</span></span><br><span class="line"><span class="string">    dtype: int64</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="number">2</span>、字典形式</span><br><span class="line">pd.Series(&#123;<span class="string">"a"</span>:<span class="number">1</span>,<span class="string">"b"</span>:<span class="number">2</span>&#125;)</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    1</span></span><br><span class="line"><span class="string">b    2</span></span><br><span class="line"><span class="string">dtype: int64</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="number">3</span>、通过index参数指定索引</span><br><span class="line">pd.Series(data=<span class="number">0</span>,index=[<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>])</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    0</span></span><br><span class="line"><span class="string">b    0</span></span><br><span class="line"><span class="string">c    0</span></span><br><span class="line"><span class="string">dtype: int64</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line">pd.Series(data=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],index=[<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>])<span class="comment"># 一一对应</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    1</span></span><br><span class="line"><span class="string">b    2</span></span><br><span class="line"><span class="string">c    3</span></span><br><span class="line"><span class="string">dtype: int64</span></span><br><span class="line"><span class="string">"""</span></span><br></pre></td></tr></table></figure>

<h3 id="2-2-Series特性"><a href="#2-2-Series特性" class="headerlink" title="2.2 Series特性"></a>2.2 Series特性</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Series支持数组的特性：</span><br><span class="line">    从ndarray创建Series：Series(arr)</span><br><span class="line">    与标量运算：sr*<span class="number">2</span></span><br><span class="line">    两个Series运算：sr1+sr2</span><br><span class="line">    索引：sr[<span class="number">0</span>], sr[[<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>]] </span><br><span class="line">    切片：sr[<span class="number">0</span>:<span class="number">2</span>]（切片依然是视图形式）</span><br><span class="line">    通用函数：np.abs(sr)</span><br><span class="line">    布尔值过滤：sr[sr&gt;<span class="number">0</span>]</span><br><span class="line">统计函数：</span><br><span class="line">    mean() <span class="comment">#求平均数</span></span><br><span class="line">    sum() <span class="comment">#求和</span></span><br><span class="line">    cumsum() <span class="comment">#累加</span></span><br><span class="line">Series支持字典的特性（标签）：</span><br><span class="line">    从字典创建Series：Series(dic),</span><br><span class="line">    <span class="keyword">in</span>运算：’a’ <span class="keyword">in</span> sr、<span class="keyword">for</span> x <span class="keyword">in</span> sr</span><br><span class="line">    键索引：sr[<span class="string">'a'</span>], sr[[<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'d'</span>]]</span><br><span class="line">    键切片：sr[<span class="string">'a'</span>:<span class="string">'c'</span>]</span><br><span class="line">    其他函数：get(<span class="string">'a'</span>, default=<span class="number">0</span>)等</span><br></pre></td></tr></table></figure>

<p>1、 统计函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">s = pd.Series(&#123;<span class="string">"a"</span>:<span class="number">1</span>,<span class="string">"b"</span>:<span class="number">2</span>,<span class="string">"c"</span>:<span class="number">3</span>,<span class="string">"d"</span>:<span class="number">4</span>&#125;)</span><br><span class="line">print(s.mean())</span><br><span class="line">print(s.sum())</span><br><span class="line">print(s.cumsum())</span><br></pre></td></tr></table></figure>

<p>2、字典特性</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1、生成数组</span></span><br><span class="line">dic = &#123;<span class="string">"a"</span>:<span class="number">1</span>,<span class="string">"b"</span>:<span class="number">2</span>,<span class="string">"c"</span>:<span class="number">3</span>,<span class="string">"d"</span>:<span class="number">5</span>&#125;</span><br><span class="line">arry =pd.Series(dic)</span><br><span class="line"><span class="comment">#2 键索引</span></span><br><span class="line">arry[<span class="string">"a"</span>] <span class="comment"># 1</span></span><br><span class="line">arry[[<span class="string">"a"</span>,<span class="string">"c"</span>]] <span class="comment"># 1</span></span><br><span class="line">    <span class="string">"""</span></span><br><span class="line"><span class="string">    a    1</span></span><br><span class="line"><span class="string">    c    3</span></span><br><span class="line"><span class="string">    dtype: int64</span></span><br><span class="line"><span class="string">    """</span></span><br><span class="line"><span class="comment">#3 键切片</span></span><br><span class="line">arry[<span class="string">"a"</span>:<span class="string">"c"</span>] </span><br><span class="line">    <span class="string">"""</span></span><br><span class="line"><span class="string">    a    1</span></span><br><span class="line"><span class="string">    b    2</span></span><br><span class="line"><span class="string">    c    3</span></span><br><span class="line"><span class="string">    dtype: int64</span></span><br><span class="line"><span class="string">    """</span></span><br><span class="line"><span class="comment"># 4get函数方法</span></span><br><span class="line">arry.get(<span class="string">"h"</span>,default=<span class="string">"nihao"</span>) </span><br><span class="line">    <span class="string">"""</span></span><br><span class="line"><span class="string">    'nihao'</span></span><br><span class="line"><span class="string">    """</span></span><br></pre></td></tr></table></figure>

<h3 id="2-3、整数索引"><a href="#2-3、整数索引" class="headerlink" title="2.3、整数索引"></a>2.3、整数索引</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">loc属性与iloc下标</span><br><span class="line"><span class="comment">#生成数组</span></span><br><span class="line">dic = &#123;<span class="string">"a"</span>:<span class="number">1</span>,<span class="string">"b"</span>:<span class="number">2</span>,<span class="string">"c"</span>:<span class="number">3</span>,<span class="string">"d"</span>:<span class="number">5</span>&#125;</span><br><span class="line">arry =pd.Series(dic)</span><br><span class="line"><span class="comment">#取出 "d":5</span></span><br><span class="line">arry.loc[<span class="string">"d"</span>]</span><br><span class="line">arry[<span class="number">-1</span>]</span><br><span class="line">arry.iloc[<span class="number">-1</span>]</span><br></pre></td></tr></table></figure>

<h3 id="2-4数据对齐"><a href="#2-4数据对齐" class="headerlink" title="2.4数据对齐"></a>2.4数据对齐</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">pandas在运算时，会按索引进行对齐然后计算。如果存在不同的索引，则结果的索引是两个操作数索引的并集。</span><br><span class="line">sr1 = pd.Series([<span class="number">12</span>,<span class="number">23</span>,<span class="number">34</span>], index=[<span class="string">'c'</span>,<span class="string">'a'</span>,<span class="string">'d'</span>])</span><br><span class="line">sr2 = pd.Series([<span class="number">11</span>,<span class="number">20</span>,<span class="number">10</span>], index=[<span class="string">'d'</span>,<span class="string">'c'</span>,<span class="string">'a'</span>,])</span><br><span class="line">sr1+sr2</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    33</span></span><br><span class="line"><span class="string">c    32</span></span><br><span class="line"><span class="string">d    45</span></span><br><span class="line"><span class="string">dtype: int64</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line">sr3 = pd.Series([<span class="number">11</span>,<span class="number">20</span>,<span class="number">10</span>,<span class="number">14</span>], index=[<span class="string">'d'</span>,<span class="string">'c'</span>,<span class="string">'a'</span>,<span class="string">'b'</span>])</span><br><span class="line">sr1+sr3</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    33.0</span></span><br><span class="line"><span class="string">b     NaN</span></span><br><span class="line"><span class="string">c    32.0</span></span><br><span class="line"><span class="string">d    45.0</span></span><br><span class="line"><span class="string">dtype: float64</span></span><br><span class="line"><span class="string">"""</span></span><br></pre></td></tr></table></figure>

<h3 id="2-5-Series缺失数据"><a href="#2-5-Series缺失数据" class="headerlink" title="2.5 Series缺失数据"></a>2.5 Series缺失数据</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、缺失数据：使用NaN（Not a Number）来表示缺失数据。其值等于np.nan。内置的<span class="literal">None</span>值也会被当做NaN处理。</span><br><span class="line"><span class="number">2</span>、处理缺失数据的相关方法：</span><br><span class="line"></span><br><span class="line">dropna()	过滤掉值为NaN的行</span><br><span class="line">fillna()	填充缺失数据</span><br><span class="line">isnull()	返回布尔数组，缺失值对应为<span class="literal">True</span></span><br><span class="line">notnull()	返回布尔数组，缺失值对应为<span class="literal">False</span></span><br><span class="line"><span class="number">3</span>、过滤缺失数据：sr.dropna() 或 sr[data.notnull()]</span><br><span class="line"><span class="number">4</span>、填充缺失数据：fillna(<span class="number">0</span>)</span><br><span class="line"><span class="comment">#生成数组</span></span><br><span class="line">sr2 = pd.Series([np.nan,<span class="number">20</span>,<span class="number">10</span>], index=[<span class="string">'d'</span>,<span class="string">'c'</span>,<span class="string">'a'</span>,])<span class="comment"># np 为numpy</span></span><br><span class="line"><span class="comment"># 过滤掉缺失的数据</span></span><br><span class="line">sr2.dropna()</span><br><span class="line">sr2[sr2.notnull()]</span><br><span class="line"><span class="comment">#填充缺失数据</span></span><br><span class="line">sr2.fillna(<span class="number">3</span>)</span><br><span class="line">sr2.fillna(methods=<span class="string">"bfill"</span>) <span class="comment">#填充的值和列表的该索引的后一位索引的值相等</span></span><br></pre></td></tr></table></figure>

<h2 id="三、DataFrame"><a href="#三、DataFrame" class="headerlink" title="三、DataFrame"></a>三、DataFrame</h2><h3 id="3-1创建方式"><a href="#3-1创建方式" class="headerlink" title="3.1创建方式"></a>3.1创建方式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">DataFrame是一个表格型的数据结构，含有一组有序的列。</span><br><span class="line">DataFrame可以被看做是由Series组成的字典，并且共用一个索引。</span><br><span class="line"></span><br><span class="line">创建方式：</span><br><span class="line"></span><br><span class="line">pd.DataFrame(&#123;<span class="string">'A'</span>:[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>],<span class="string">'B'</span>:[<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>]&#125;)</span><br><span class="line">pd.DataFrame(&#123;<span class="string">'A'</span>:pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],index=[<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>]), <span class="string">'B'</span>:pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>],index=[<span class="string">'b'</span>,<span class="string">'a'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>])&#125;)</span><br><span class="line">df = pd.DataFrame(data=np.random.randint(<span class="number">1</span>,<span class="number">10</span>,size=(<span class="number">4</span>,<span class="number">5</span>)),index=[<span class="string">"A"</span>,<span class="string">"B"</span>,<span class="string">"C"</span>,<span class="string">"D"</span>],</span><br><span class="line">                  columns=[<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>,<span class="string">"e"</span>])</span><br><span class="line">……</span><br><span class="line">csv文件读取与写入：</span><br><span class="line"></span><br><span class="line">df.read_csv(<span class="string">'./601318.csv'</span>)</span><br><span class="line">df.to_csv()</span><br></pre></td></tr></table></figure>

<h3 id="3-2、DataFrame查看数据"><a href="#3-2、DataFrame查看数据" class="headerlink" title="3.2、DataFrame查看数据"></a>3.2、DataFrame查看数据</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">查看数据常用属性及方法：</span><br><span class="line">        index                      获取索引</span><br><span class="line">        T                          转置</span><br><span class="line">        columns                    获取列索引</span><br><span class="line">        values                     获取值数组</span><br><span class="line">        describe()                 获取快速统计</span><br><span class="line"></span><br><span class="line">    DataFrame各列name属性：给列属性重命名</span><br><span class="line">    df.rename(columns=&#123;<span class="string">"旧字段的名字"</span>:<span class="string">"新的名字"</span>&#125;)</span><br></pre></td></tr></table></figure>

<p>代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#生成数组</span></span><br><span class="line">df =pd.DataFrame(&#123;<span class="string">'A'</span>:pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>],index=[<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>]), <span class="string">'B'</span>:pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>],index=[<span class="string">'b'</span>,<span class="string">'a'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>])&#125;)</span><br><span class="line"><span class="comment">#获取索引</span></span><br><span class="line">df.index <span class="comment">#Index(['a', 'b', 'c', 'd'], dtype='object')</span></span><br><span class="line"><span class="comment">#转置</span></span><br><span class="line">df.T</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">	a	b	c	d</span></span><br><span class="line"><span class="string">A	1.0	2.0	3.0	NaN</span></span><br><span class="line"><span class="string">B	2.0	1.0	3.0	4.0</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="comment">#获取列索引</span></span><br><span class="line">df.T  <span class="comment">#Index(['A', 'B'], dtype='object')</span></span><br><span class="line"><span class="comment">#获取值数组</span></span><br><span class="line">df.T</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">array([[ 1.,  2.],</span></span><br><span class="line"><span class="string">       [ 2.,  1.],</span></span><br><span class="line"><span class="string">       [ 3.,  3.],</span></span><br><span class="line"><span class="string">       [nan,  4.]])</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="comment">#获取快速统计</span></span><br><span class="line">df.describe()</span><br><span class="line">    <span class="string">"""</span></span><br><span class="line"><span class="string">    A	B</span></span><br><span class="line"><span class="string">    count	3.0	4.000000</span></span><br><span class="line"><span class="string">    mean	2.0	2.500000</span></span><br><span class="line"><span class="string">    std	1.0	1.290994</span></span><br><span class="line"><span class="string">    min	1.0	1.000000</span></span><br><span class="line"><span class="string">    25%	1.5	1.750000</span></span><br><span class="line"><span class="string">    50%	2.0	2.500000</span></span><br><span class="line"><span class="string">    75%	2.5	3.250000</span></span><br><span class="line"><span class="string">    max	3.0	4.000000</span></span><br><span class="line"><span class="string">    """</span></span><br><span class="line"><span class="comment">#DataFrame各列name属性C</span></span><br><span class="line">df.rename(columns=&#123;<span class="string">"A"</span>:<span class="string">"a"</span>&#125;)</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a	B</span></span><br><span class="line"><span class="string">a	1.0	2</span></span><br><span class="line"><span class="string">b	2.0	1</span></span><br><span class="line"><span class="string">c	3.0	3</span></span><br><span class="line"><span class="string">d	NaN	4</span></span><br><span class="line"><span class="string">"""</span></span><br></pre></td></tr></table></figure>

<h3 id="3-3DataFrame索引和切片"><a href="#3-3DataFrame索引和切片" class="headerlink" title="3.3DataFrame索引和切片"></a>3.3DataFrame索引和切片</h3><p>1、说明</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、DataFrame有行索引和列索引。</span><br><span class="line"><span class="number">2</span>、DataFrame同样可以通过标签和位置两种方法进行索引和切片。</span><br><span class="line"><span class="number">3</span>、DataFrame使用索引切片：</span><br><span class="line"></span><br><span class="line">方法<span class="number">1</span>：两个中括号，先取列再取行。	df[<span class="string">'A'</span>][<span class="number">0</span>]</span><br><span class="line">方法<span class="number">2</span>（推荐）：使用loc/iloc属性，一个中括号，逗号隔开，先取行再取列。</span><br><span class="line">    loc属性：解释为标签</span><br><span class="line">    iloc属性：解释为下标</span><br><span class="line">向DataFrame对象中写入值时只使用方法<span class="number">2</span></span><br><span class="line">	行/列索引部分可以是常规索引、切片、布尔值索引、花式索引任意搭配。（注意：两部分都是花式索引时结果可能与预料的不同）</span><br><span class="line">总结：</span><br><span class="line">	<span class="number">1</span> df[<span class="string">"索引"</span>]时：</span><br><span class="line">    	列索引默认是显性索引，使用隐形索引会报错</span><br><span class="line">        行索引默认是隐形索引，使用显性索引会报错</span><br><span class="line">    <span class="number">2</span>、loc/iloc</span><br><span class="line">    	使用方法df.loc[<span class="string">"A"</span>,<span class="string">"a"</span>] 或df.loc[[<span class="string">"A"</span>,<span class="string">"B"</span>],[<span class="string">"a"</span>,<span class="string">"c"</span>]]</span><br><span class="line">        	   df.iloc[<span class="string">"A"</span>,<span class="string">"a"</span>] 或df.iloc[[<span class="string">"A"</span>,<span class="string">"B"</span>],[<span class="string">"a"</span>,<span class="string">"c"</span>]]</span><br><span class="line">        loc 的【】只能是显性索引</span><br><span class="line">        iloc [] 里面智能是隐形索引</span><br><span class="line">        ，改成：就从多个取值变成了切片</span><br><span class="line">        	使用iloc切片时，顾头不顾尾</span><br><span class="line">        	使用loc切片时，顾头顾尾</span><br></pre></td></tr></table></figure>

<p>2、code</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1、生成数组</span></span><br><span class="line">df = pd.DataFrame(data=np.random.randint(<span class="number">1</span>,<span class="number">10</span>,size=(<span class="number">4</span>,<span class="number">5</span>)),index=[<span class="string">"A"</span>,<span class="string">"B"</span>,<span class="string">"C"</span>,<span class="string">"D"</span>],</span><br><span class="line">                  columns=[<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>,<span class="string">"e"</span>])</span><br><span class="line"><span class="comment">#取值</span></span><br><span class="line">df[<span class="string">'a'</span>][<span class="number">0</span>] <span class="comment">#可以取值</span></span><br><span class="line"><span class="comment"># 错误取法df['a'][“A"]  df[0][“A"] df[0][0] </span></span><br><span class="line"><span class="comment">#loc/iloc 后面只能接列表loc里面放的显性索引</span></span><br><span class="line">df.loc[<span class="string">"A"</span>,<span class="string">"a"</span>]</span><br><span class="line">df.iloc[<span class="number">1</span>,<span class="number">2</span>]</span><br><span class="line"><span class="comment">#切片</span></span><br><span class="line">df.iloc[<span class="number">1</span>:<span class="number">2</span>]</span><br><span class="line">df.iloc[<span class="number">0</span>:<span class="number">3</span>,<span class="number">0</span>:<span class="number">3</span>] <span class="comment"># 顾头不顾尾</span></span><br><span class="line">df.loc[<span class="string">"A"</span>:<span class="string">"B"</span>,<span class="string">"a"</span>:<span class="string">"c"</span>]<span class="comment"># 顾头顾尾</span></span><br></pre></td></tr></table></figure>

<h3 id="3-4-DataFrame数据对齐与缺失数据"><a href="#3-4-DataFrame数据对齐与缺失数据" class="headerlink" title="3.4 DataFrame数据对齐与缺失数据"></a>3.4 DataFrame数据对齐与缺失数据</h3><p>1、解释</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">DataFrame对象在运算时，同样会进行数据对齐，行索引与列索引分别对齐。</span><br><span class="line">结果的行索引与列索引分别为两个操作数的行索引与列索引的并集。</span><br><span class="line"></span><br><span class="line">DataFrame处理缺失数据的相关方法：</span><br><span class="line"></span><br><span class="line">dropna(axis=<span class="number">0</span>,where=‘any’,…) 过滤掉值为NaN的行</span><br><span class="line">    fillna()	填充缺失数据</span><br><span class="line">    isnull()	返回布尔数组，缺失值对应为<span class="literal">True</span> 与any一起使用</span><br><span class="line">    notnull()	返回布尔数组，缺失值对应为<span class="literal">False</span> 与all一起使用</span><br><span class="line"></span><br><span class="line">axis  正常 <span class="number">0</span> 代表列 <span class="number">1</span> 代表行</span><br><span class="line">	  在dropna中 <span class="number">0</span> 代表行 <span class="number">1</span> 代表列</span><br></pre></td></tr></table></figure>

<p>2、代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#数据对齐</span></span><br><span class="line"><span class="comment">#（开盘 - 前日收盘） / 前日收盘  &lt; -0.02</span></span><br><span class="line">df.loc[(df[<span class="string">'open'</span>] - df[<span class="string">'close'</span>].shift(<span class="number">1</span>)) / df[<span class="string">'close'</span>].shift(<span class="number">1</span>)</span><br><span class="line"><span class="comment">#数据缺失的处理</span></span><br><span class="line"><span class="comment">#去掉有缺失的一行</span></span><br><span class="line">    df.loc[df.notnull().all(axis=<span class="number">1</span>)]</span><br><span class="line">    df.dropna(axis=<span class="number">0</span>)  </span><br><span class="line"><span class="comment">#填充</span></span><br><span class="line">   df.fillna(method=<span class="string">'ffill'</span>,axis=<span class="number">0</span>) df.fillna(method=<span class="string">'ffill'</span>,axis=<span class="number">0</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-5-pandas的拼接操作"><a href="#3-5-pandas的拼接操作" class="headerlink" title="3.5 pandas的拼接操作"></a>3.5 pandas的拼接操作</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">pandas的拼接分为两种：</span><br><span class="line">	级联：pd.concat, pd.append</span><br><span class="line">	合并：pd.merge, pd.join</span><br><span class="line">objs</span><br><span class="line">axis=0</span><br><span class="line">keys</span><br><span class="line">join=&apos;outer&apos; / &apos;inner&apos;:表示的是级联的方式，outer会将所有的项进行级联（忽略匹配和不匹配），而inner只会将匹配的项级联到一起，不匹配的不级联</span><br><span class="line">ignore_index=False</span><br></pre></td></tr></table></figure>

<p>code</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1匹配级联</span></span><br><span class="line">df1 = DataFrame(data=np.random.randint(<span class="number">0</span>,<span class="number">100</span>,size=(<span class="number">3</span>,<span class="number">4</span>)),index=[<span class="string">'A'</span>,<span class="string">'B'</span>,<span class="string">'C'</span>],columns=[<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>])</span><br><span class="line">df2 = DataFrame(data=np.random.randint(<span class="number">0</span>,<span class="number">100</span>,size=(<span class="number">3</span>,<span class="number">4</span>)),index=[<span class="string">'A'</span>,<span class="string">'D'</span>,<span class="string">'C'</span>],columns=[<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'e'</span>,<span class="string">'d'</span>])</span><br><span class="line">display(df1,df2)</span><br><span class="line">pd.concat((df1,df1,df1),axis=<span class="number">1</span>,join=<span class="string">'inner'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#2不匹配级联</span></span><br><span class="line">不匹配指的是级联的维度的索引不一致。例如纵向级联时列索引不一致，横向级联时行索引不一致</span><br><span class="line">pd.concat((df1,df2),axis=<span class="number">0</span>,join=<span class="string">'inner'</span>)</span><br><span class="line">pd.concat((df1,df2),axis=<span class="number">0</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-6merge-合并"><a href="#3-6merge-合并" class="headerlink" title="3.6merge 合并"></a>3.6merge 合并</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">how：out取并集 inner取交集</span><br><span class="line">on：当有多列相同的时候，可以使用on来指定使用那一列进行合并，on的值为一个列表</span><br></pre></td></tr></table></figure>

<p>1、一对一合并</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">df1 = DataFrame(&#123;<span class="string">'employee'</span>:[<span class="string">'Bob'</span>,<span class="string">'Jake'</span>,<span class="string">'Lisa'</span>],</span><br><span class="line">                <span class="string">'group'</span>:[<span class="string">'Accounting'</span>,<span class="string">'Engineering'</span>,<span class="string">'Engineering'</span>],</span><br><span class="line">                &#125;)</span><br><span class="line">df2 = DataFrame(&#123;<span class="string">'employee'</span>:[<span class="string">'Lisa'</span>,<span class="string">'Bob'</span>,<span class="string">'Jake'</span>],</span><br><span class="line">                <span class="string">'hire_date'</span>:[<span class="number">2004</span>,<span class="number">2008</span>,<span class="number">2012</span>],</span><br><span class="line">                &#125;)</span><br><span class="line">pd.merge(df1,df2)</span><br></pre></td></tr></table></figure>

<p>2、多对一合并</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">df3 = DataFrame(&#123;</span><br><span class="line">    &apos;employee&apos;:[&apos;Lisa&apos;,&apos;Jake&apos;],</span><br><span class="line">    &apos;group&apos;:[&apos;Accounting&apos;,&apos;Engineering&apos;],</span><br><span class="line">    &apos;hire_date&apos;:[2004,2016]&#125;)</span><br><span class="line">df4 = DataFrame(&#123;&apos;group&apos;:[&apos;Accounting&apos;,&apos;Engineering&apos;,&apos;Engineering&apos;],</span><br><span class="line">                       &apos;supervisor&apos;:[&apos;Carly&apos;,&apos;Guido&apos;,&apos;Steve&apos;]</span><br><span class="line">                &#125;)</span><br><span class="line">pd.merge(df3,df4)</span><br></pre></td></tr></table></figure>

<p>3、多对多合并</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">df1 = DataFrame(&#123;<span class="string">'employee'</span>:[<span class="string">'Bob'</span>,<span class="string">'Jake'</span>,<span class="string">'Lisa'</span>],</span><br><span class="line">                 <span class="string">'group'</span>:[<span class="string">'Accounting'</span>,<span class="string">'Engineering'</span>,<span class="string">'Engineering'</span>]&#125;)</span><br><span class="line">df5 = DataFrame(&#123;<span class="string">'group'</span>:[<span class="string">'Engineering'</span>,<span class="string">'Engineering'</span>,<span class="string">'HR'</span>],</span><br><span class="line">                <span class="string">'supervisor'</span>:[<span class="string">'Carly'</span>,<span class="string">'Guido'</span>,<span class="string">'Steve'</span>]</span><br><span class="line">                &#125;)</span><br><span class="line">pd.merge(df1,df5,how=<span class="string">'outer'</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-7key的规范化"><a href="#3-7key的规范化" class="headerlink" title="3.7key的规范化"></a>3.7key的规范化</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">当列冲突时，即有多个列名称相同时，需要使用on=来指定哪一个列作为key，配合suffixes指定冲突列名</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">df1 = DataFrame(&#123;&apos;employee&apos;:[&apos;Jack&apos;,&quot;Summer&quot;,&quot;Steve&quot;],</span><br><span class="line">                 &apos;group&apos;:[&apos;Accounting&apos;,&apos;Finance&apos;,&apos;Marketing&apos;]&#125;)</span><br><span class="line">df2 = DataFrame(&#123;&apos;employee&apos;:[&apos;Jack&apos;,&apos;Bob&apos;,&quot;Jake&quot;],</span><br><span class="line">                 &apos;hire_date&apos;:[2003,2009,2012],</span><br><span class="line">                &apos;group&apos;:[&apos;Accounting&apos;,&apos;sell&apos;,&apos;ceo&apos;]&#125;)</span><br><span class="line">pd.merge(df1,df2,on=&apos;employee&apos;)</span><br></pre></td></tr></table></figure>

<h3 id="3-8-内合并与外合并"><a href="#3-8-内合并与外合并" class="headerlink" title="3.8 内合并与外合并"></a>3.8 内合并与外合并</h3><p>1、内合并</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">df6 = DataFrame(&#123;<span class="string">'name'</span>:[<span class="string">'Peter'</span>,<span class="string">'Paul'</span>,<span class="string">'Mary'</span>],</span><br><span class="line">               <span class="string">'food'</span>:[<span class="string">'fish'</span>,<span class="string">'beans'</span>,<span class="string">'bread'</span>]&#125;</span><br><span class="line">               )</span><br><span class="line">df7 = DataFrame(&#123;<span class="string">'name'</span>:[<span class="string">'Mary'</span>,<span class="string">'Joseph'</span>],</span><br><span class="line">                <span class="string">'drink'</span>:[<span class="string">'wine'</span>,<span class="string">'beer'</span>]&#125;)</span><br><span class="line">pd.merge(df6,df7)</span><br></pre></td></tr></table></figure>

<p>2、外合并</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">df6 = DataFrame(&#123;<span class="string">'name'</span>:[<span class="string">'Peter'</span>,<span class="string">'Paul'</span>,<span class="string">'Mary'</span>],</span><br><span class="line">               <span class="string">'food'</span>:[<span class="string">'fish'</span>,<span class="string">'beans'</span>,<span class="string">'bread'</span>]&#125;</span><br><span class="line">               )</span><br><span class="line">df7 = DataFrame(&#123;<span class="string">'name'</span>:[<span class="string">'Mary'</span>,<span class="string">'Joseph'</span>],</span><br><span class="line">                <span class="string">'drink'</span>:[<span class="string">'wine'</span>,<span class="string">'beer'</span>]&#125;)</span><br><span class="line">pd.merge(df6,df7，how=<span class="string">"outer"</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-8删除重复元素"><a href="#3-8删除重复元素" class="headerlink" title="3.8删除重复元素"></a>3.8删除重复元素</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">df = DataFrame(data=np.random.randint(<span class="number">0</span>,<span class="number">100</span>,size=(<span class="number">9</span>,<span class="number">5</span>)))</span><br><span class="line">df.iloc[<span class="number">1</span>] = [<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>]</span><br><span class="line">df.iloc[<span class="number">3</span>] = [<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>]</span><br><span class="line">df.iloc[<span class="number">5</span>] = [<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>]</span><br><span class="line">df.drop_duplicates(keep=<span class="string">'first'</span>)</span><br><span class="line">解释：</span><br><span class="line"> - keep参数：指定保留哪一重复的行数据</span><br></pre></td></tr></table></figure>

<h3 id="3-9映射"><a href="#3-9映射" class="headerlink" title="3.9映射"></a>3.9映射</h3><p>1、replace函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">单值替换</span><br><span class="line">普通替换： 替换所有符合要求的元素:to_replace=<span class="number">15</span>,value=<span class="string">'e'</span></span><br><span class="line">按列指定单值替换： to_replace=&#123;列标签：替换值&#125; value=<span class="string">'value'</span></span><br><span class="line">多值替换</span><br><span class="line">列表替换: to_replace=[] value=[]</span><br><span class="line">字典替换（推荐） to_replace=&#123;to_replace:value,to_replace:value&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1、单值替换</span><br><span class="line">df.replace(to_replace=6,value=&apos;six&apos;)</span><br><span class="line">df.replace(to_replace=&#123;3:6&#125;,value=&apos;six&apos;)</span><br><span class="line">2、多值替换</span><br><span class="line">df.replace(to_replace=[3,66],value=[&quot;1&quot;,&quot;2&quot;])</span><br><span class="line">df.replace(to_replace=&#123;95:26,70:123&#125;)</span><br></pre></td></tr></table></figure>

<p>2、map</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">map()可以映射新一列数据</span><br><span class="line">map()中可以使用lambd表达式</span><br><span class="line">map()中可以使用方法，可以是自定义的方法</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">dic = &#123;</span><br><span class="line">    <span class="string">'name'</span>:[<span class="string">'jay'</span>,<span class="string">'tom'</span>,<span class="string">'jay'</span>],</span><br><span class="line">    <span class="string">'salary'</span>:[<span class="number">9999</span>,<span class="number">5000</span>,<span class="number">9999</span>]</span><br><span class="line">&#125;</span><br><span class="line">df = DataFrame(data=dic)</span><br><span class="line">dic = &#123;</span><br><span class="line">    <span class="string">'jay'</span>:<span class="string">'周杰伦'</span>,</span><br><span class="line">    <span class="string">'tom'</span>:<span class="string">'张三'</span></span><br><span class="line">&#125;</span><br><span class="line">df[<span class="string">'c_name'</span>] = df[<span class="string">'name'</span>].map(dic)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">after_sal</span><span class="params">(s)</span>:</span></span><br><span class="line">    <span class="keyword">return</span> s - (s<span class="number">-5000</span>)*<span class="number">0.5</span></span><br><span class="line"></span><br><span class="line">df[<span class="string">'after_sal'</span>] = df[<span class="string">'salary'</span>].map(after_sal)</span><br><span class="line">df[<span class="string">'salary'</span>].apply(after_sal)</span><br></pre></td></tr></table></figure>

<h3 id="3-10使用聚合操作对数据异常值检测和过滤"><a href="#3-10使用聚合操作对数据异常值检测和过滤" class="headerlink" title="3.10使用聚合操作对数据异常值检测和过滤"></a>3.10使用聚合操作对数据异常值检测和过滤</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df = DataFrame(data=np.random.random(size=(<span class="number">1000</span>,<span class="number">3</span>)),columns=[<span class="string">'A'</span>,<span class="string">'B'</span>,<span class="string">'C'</span>])</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">df[<span class="string">'C'</span>] &gt; std_twice</span><br><span class="line"><span class="comment">#异常值对应的行数据</span></span><br><span class="line">df.loc[df[<span class="string">'C'</span>] &gt; std_twice]</span><br><span class="line">indexs = df.loc[df[<span class="string">'C'</span>] &gt; std_twice].index</span><br><span class="line">df.drop(labels=indexs,axis=<span class="number">0</span>,inplace=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-11排序"><a href="#3-11排序" class="headerlink" title="3.11排序"></a>3.11排序</h3><p>1、参数说明</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">使用.take()函数排序</span><br><span class="line">	- take()函数接受一个索引列表，用数字表示,使得df根据列表中索引的顺序进行排序</span><br><span class="line">	- eg:df.take([1,3,4,2,5])</span><br><span class="line">可以借助np.random.permutation()函数随机排序</span><br></pre></td></tr></table></figure>

<p>2、代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">df.take([2,1,0],axis=1).take(np.random.permutation(500),axis=0)</span><br><span class="line">df.take([2,1,0],axis=1).take(np.random.permutation(500),axis=0)[0:20]</span><br></pre></td></tr></table></figure>

<h3 id="3-12分组"><a href="#3-12分组" class="headerlink" title="3.12分组"></a>3.12分组</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">df = DataFrame(&#123;&apos;item&apos;:[&apos;Apple&apos;,&apos;Banana&apos;,&apos;Orange&apos;,&apos;Banana&apos;,&apos;Orange&apos;,&apos;Apple&apos;],</span><br><span class="line">                &apos;price&apos;:[4,3,3,2.5,4,2],</span><br><span class="line">               &apos;color&apos;:[&apos;red&apos;,&apos;yellow&apos;,&apos;yellow&apos;,&apos;green&apos;,&apos;green&apos;,&apos;green&apos;],</span><br><span class="line">               &apos;weight&apos;:[12,20,50,30,20,44]&#125;)</span><br><span class="line">               </span><br><span class="line">df.groupby(by=&apos;item&apos;)</span><br><span class="line">df.groupby(by=&apos;item&apos;).mean()[&apos;price&apos;]</span><br><span class="line">mean_price_s = df.groupby(by=&apos;item&apos;)[&apos;price&apos;].mean()</span><br><span class="line">dic = mean_price_s.to_dict()</span><br><span class="line">df[&apos;mean_price&apos;] = df[&apos;item&apos;].map(dic)</span><br></pre></td></tr></table></figure>

<h3 id="3-13高级数据聚合"><a href="#3-13高级数据聚合" class="headerlink" title="3.13高级数据聚合"></a>3.13高级数据聚合</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">使用groupby分组后，也可以使用transform和apply提供自定义函数实现更多的运算</span><br><span class="line">    df.groupby('item')['price'].sum() &lt;==&gt; df.groupby('item')['price'].apply(sum)</span><br><span class="line">    transform和apply都会进行运算，在transform或者apply中传入函数即可</span><br><span class="line">    transform和apply也可以传入一个<span class="keyword">lambda</span>表达式</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">my_mean</span><span class="params">(s)</span>:</span></span><br><span class="line">    sum = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> s:</span><br><span class="line">        sum += i</span><br><span class="line">    <span class="keyword">return</span> sum/s.size</span><br><span class="line">df.groupby(by=<span class="string">'item'</span>)[<span class="string">'price'</span>].transform(my_mean)</span><br><span class="line">df.groupby(by=<span class="string">'item'</span>)[<span class="string">'price'</span>].apply(my_mean)</span><br></pre></td></tr></table></figure>

<h2 id="四、pandas其他操作"><a href="#四、pandas其他操作" class="headerlink" title="四、pandas其他操作"></a>四、pandas其他操作</h2><h3 id="4-1、Series数据对齐"><a href="#4-1、Series数据对齐" class="headerlink" title="4.1、Series数据对齐"></a>4.1、Series数据对齐</h3><p>1、对齐操作</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">pandas在运算时，会按索引进行对齐然后计算。如果存在不同的索引，则结果的索引是两个操作数索引的并集</span><br><span class="line">sr1 = pd.Series([<span class="number">12</span>,<span class="number">23</span>,<span class="number">34</span>], index=[<span class="string">'c'</span>,<span class="string">'a'</span>,<span class="string">'d'</span>])</span><br><span class="line">sr2 = pd.Series([<span class="number">11</span>,<span class="number">20</span>,<span class="number">10</span>], index=[<span class="string">'d'</span>,<span class="string">'c'</span>,<span class="string">'a'</span>,])</span><br><span class="line">print(sr1+sr2)</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    33</span></span><br><span class="line"><span class="string">c    32</span></span><br><span class="line"><span class="string">d    45</span></span><br><span class="line"><span class="string">dtype: int64</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line">sr3 = pd.Series([<span class="number">11</span>,<span class="number">20</span>,<span class="number">10</span>,<span class="number">14</span>], index=[<span class="string">'d'</span>,<span class="string">'c'</span>,<span class="string">'a'</span>,<span class="string">'b'</span>])</span><br><span class="line">print(sr1+sr3)</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    33.0</span></span><br><span class="line"><span class="string">b     NaN</span></span><br><span class="line"><span class="string">c    32.0</span></span><br><span class="line"><span class="string">d    45.0</span></span><br><span class="line"><span class="string">dtype: float64</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line">print(sr1.add(sr2, fill_value=<span class="number">0</span>))</span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">a    33</span></span><br><span class="line"><span class="string">c    32</span></span><br><span class="line"><span class="string">d    45</span></span><br><span class="line"><span class="string">dtype: int64</span></span><br><span class="line"><span class="string">"""</span></span><br></pre></td></tr></table></figure>

<h3 id="4-2-Series缺失数据"><a href="#4-2-Series缺失数据" class="headerlink" title="4.2 Series缺失数据"></a>4.2 Series缺失数据</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1、缺失数据：使用NaN（Not a Number）来表示缺失数据。其值等于np.nan。内置的None值也会被当做NaN处理。</span><br><span class="line">2、处理缺失数据的相关方法：</span><br><span class="line">    dropna()	过滤掉值为NaN的行</span><br><span class="line">    fillna()	填充缺失数据</span><br><span class="line">    isnull()	返回布尔数组，缺失值对应为True</span><br><span class="line">    notnull()	返回布尔数组，缺失值对应为False</span><br><span class="line">3、过滤缺失数据：sr.dropna() 或 sr[data.notnull()]</span><br><span class="line">4、填充缺失数据：fillna(0)</span><br></pre></td></tr></table></figure>

<h2 id="五、其他操作"><a href="#五、其他操作" class="headerlink" title="五、其他操作"></a>五、其他操作</h2><h3 id="5-1时间对象处理"><a href="#5-1时间对象处理" class="headerlink" title="5.1时间对象处理"></a>5.1时间对象处理</h3><p>1、介绍</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">时间序列类型：</span><br><span class="line">    时间戳：特定时刻</span><br><span class="line">    固定时期：如2017年7月</span><br><span class="line">    时间间隔：起始时间-结束时间</span><br><span class="line">Python标准库：datetime</span><br><span class="line">    datetime.datetime.timedelta  # 表示 时间间隔</span><br><span class="line">    dt.strftime() #f：format吧时间对象格式化成字符串</span><br><span class="line">    strptime()  #吧字符串解析成时间对象p：parse</span><br><span class="line">    灵活处理时间对象：dateutil包</span><br><span class="line">        dateutil.parser.parse(&apos;2018/1/29&apos;) </span><br><span class="line">    成组处理时间对象：pandas</span><br><span class="line">        pd.to_datetime([&apos;2001-01-01&apos;, &apos;2002-02-02&apos;])</span><br><span class="line"> 产生时间的对象数组date_range</span><br><span class="line">    start 开始时间</span><br><span class="line">    end 结束时间</span><br><span class="line">    periods 时间长度</span><br><span class="line">    freq 时间频率，默认为&apos;D&apos;，可选H(our),W(eek),B(usiness),S(emi-)M(onth),(min)T(es), S(econd), A(year),…</span><br></pre></td></tr></table></figure>

<p>2、时间序列</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">1、时间序列就是以时间对象为索引的Series或DataFrame。</span><br><span class="line">2、datetime对象作为索引时是存储在DatetimeIndex对象中的。</span><br><span class="line">3、时间序列特殊功能：</span><br><span class="line">传入“年”或“年月”作为切片方式</span><br><span class="line">传入日期范围作为切片方式</span><br><span class="line">丰富的函数支持：resample(), strftime(), ……</span><br><span class="line">批量转换为datetime对象：to_pydatetime()</span><br></pre></td></tr></table></figure>

<h3 id="5-2从文件读取"><a href="#5-2从文件读取" class="headerlink" title="5.2从文件读取"></a>5.2从文件读取</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、时间序列就是以时间对象作为索引</span><br><span class="line">    读取文件：从文件名、URL、文件对象中加载数据</span><br><span class="line">    read_csv 默认分隔符为csv</span><br><span class="line">    read_table 默认分隔符为\t</span><br><span class="line">    read_excel 读取excel文件</span><br><span class="line"><span class="number">2</span>、读取文件函数主要参数：</span><br><span class="line">    sep 指定分隔符，可用正则表达式如<span class="string">'\s+'</span></span><br><span class="line">    header=<span class="literal">None</span> 指定文件无列名</span><br><span class="line">    name 指定列名</span><br><span class="line">    index_col 指定某列作为索引</span><br><span class="line">    skip_row 指定跳过某些行</span><br><span class="line">    na_values 指定某些字符串表示缺失值</span><br><span class="line">    parse_dates 指定某些列是否被解析为日期，布尔值或列表</span><br></pre></td></tr></table></figure>

<h2 id="六总结"><a href="#六总结" class="headerlink" title="六总结"></a>六总结</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">组成：</span><br><span class="line">	values：一组数据（ndarray类型）</span><br><span class="line">	index：相关的数据索引标签</span><br><span class="line">创建方式：</span><br><span class="line">	Series(data=[1,2,3])</span><br><span class="line">	s = Series(data=[1,2,3],index=[&apos;a&apos;,&apos;b&apos;,&apos;c&apos;])</span><br><span class="line">索引和切片：</span><br><span class="line">	</span><br><span class="line">基本操作：</span><br><span class="line">	查看前n个值   s.head(2)</span><br><span class="line">	查看前n个值   s.tail(2)</span><br><span class="line">	去重：</span><br><span class="line">		s = Series(data=[1,1,2,2,3,4,5,6,6,6,7,6,6,7,8])</span><br><span class="line">		s.unique()</span><br><span class="line">	检测缺失：</span><br><span class="line">		s.isnull() 数据没有 False</span><br><span class="line">		s.notnull() 数据有为 True</span><br><span class="line">DataFrame</span><br><span class="line">	创建：</span><br><span class="line">		最常用的方法是传递一个字典来创建</span><br><span class="line">		DataFrame(data=np.random.randint(60,100,size=(3,4)))</span><br><span class="line">	属性：</span><br><span class="line">		values、columns、index、shape</span><br><span class="line">	索引：</span><br><span class="line">		行索引：index</span><br><span class="line">			  获取第一行 df.iloc[0] df.loc[&apos;A&apos;]（某一行）</span><br><span class="line">			  获取前两行 df.loc[[&quot;A&quot;,&quot;B&quot;]] </span><br><span class="line">		列索引：columns</span><br><span class="line">			修改列索引：df.columns = [&apos;a&apos;,&apos;c&apos;,&apos;b&apos;,&apos;d&apos;]</span><br><span class="line">			获取前两列：df[[&apos;a&apos;,&apos;c&apos;]]</span><br><span class="line">	切片：</span><br><span class="line">        前两行	df[0:2]</span><br><span class="line">        前两列 df.iloc[:,0:2]</span><br><span class="line">        某一列：df[&apos;a&apos;]</span><br><span class="line">	值：values</span><br><span class="line">        df.iloc[1,2]</span><br><span class="line">        df.loc[[&apos;B&apos;,&apos;C&apos;],&apos;b&apos;]</span><br><span class="line">    运算：</span><br><span class="line">    	直接：直接相加</span><br><span class="line">....待更新</span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://yoursite.com/2019/09/20/【数据分析02】数据分析二/" title="数据分析pandas">http://yoursite.com/2019/09/20/【数据分析02】数据分析二/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/dxl/tags/爬虫/" rel="tag"># 爬虫</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/dxl/2019/09/20/【数据分析01】数据分析一/" rel="next" title="数据分析numpy">
                <i class="fa fa-chevron-left"></i> 数据分析numpy
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/dxl/2019/09/20/【数据分析03】数据分析三/" rel="prev" title="数据分析Matplotlib">
                数据分析Matplotlib <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/dxl/images/avatar.png" alt>
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/dxl/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/dxl/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/dxl/tags/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-sign-out"></i>
                我的友链
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="tencent://message/?Menu=yes&uin=1258517737&Site=QQ%E6%9E%81%E5%AE%A2&Service=300&sigT=45a1e5847943b64c6ff3990f8a9e644d2b31356cb0b4ac6b24663a3c8dd0f8aa12a595b1714f9d45/" title="申请坑位" target="_blank">申请坑位</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、pandas简单介绍"><span class="nav-number">1.</span> <span class="nav-text">一、pandas简单介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、Series"><span class="nav-number">2.</span> <span class="nav-text">二、Series</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1创建Series"><span class="nav-number">2.1.</span> <span class="nav-text">2.1创建Series</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-Series特性"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 Series特性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3、整数索引"><span class="nav-number">2.3.</span> <span class="nav-text">2.3、整数索引</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4数据对齐"><span class="nav-number">2.4.</span> <span class="nav-text">2.4数据对齐</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-Series缺失数据"><span class="nav-number">2.5.</span> <span class="nav-text">2.5 Series缺失数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、DataFrame"><span class="nav-number">3.</span> <span class="nav-text">三、DataFrame</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1创建方式"><span class="nav-number">3.1.</span> <span class="nav-text">3.1创建方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2、DataFrame查看数据"><span class="nav-number">3.2.</span> <span class="nav-text">3.2、DataFrame查看数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3DataFrame索引和切片"><span class="nav-number">3.3.</span> <span class="nav-text">3.3DataFrame索引和切片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-DataFrame数据对齐与缺失数据"><span class="nav-number">3.4.</span> <span class="nav-text">3.4 DataFrame数据对齐与缺失数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-pandas的拼接操作"><span class="nav-number">3.5.</span> <span class="nav-text">3.5 pandas的拼接操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6merge-合并"><span class="nav-number">3.6.</span> <span class="nav-text">3.6merge 合并</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7key的规范化"><span class="nav-number">3.7.</span> <span class="nav-text">3.7key的规范化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-8-内合并与外合并"><span class="nav-number">3.8.</span> <span class="nav-text">3.8 内合并与外合并</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-8删除重复元素"><span class="nav-number">3.9.</span> <span class="nav-text">3.8删除重复元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-9映射"><span class="nav-number">3.10.</span> <span class="nav-text">3.9映射</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-10使用聚合操作对数据异常值检测和过滤"><span class="nav-number">3.11.</span> <span class="nav-text">3.10使用聚合操作对数据异常值检测和过滤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-11排序"><span class="nav-number">3.12.</span> <span class="nav-text">3.11排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-12分组"><span class="nav-number">3.13.</span> <span class="nav-text">3.12分组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-13高级数据聚合"><span class="nav-number">3.14.</span> <span class="nav-text">3.13高级数据聚合</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四、pandas其他操作"><span class="nav-number">4.</span> <span class="nav-text">四、pandas其他操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1、Series数据对齐"><span class="nav-number">4.1.</span> <span class="nav-text">4.1、Series数据对齐</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-Series缺失数据"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 Series缺失数据</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#五、其他操作"><span class="nav-number">5.</span> <span class="nav-text">五、其他操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1时间对象处理"><span class="nav-number">5.1.</span> <span class="nav-text">5.1时间对象处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2从文件读取"><span class="nav-number">5.2.</span> <span class="nav-text">5.2从文件读取</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#六总结"><span class="nav-number">6.</span> <span class="nav-text">六总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-hand-peace-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"></span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      本站访客数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      本站总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/dxl/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/dxl/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/dxl/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/dxl/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/dxl/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/dxl/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '13B0JGDuA6ttduN8AQaR8CzF-gzGzoHsz',
        appKey: 'I13r9r5mVgq4jQYpYy6V4gW3',
        placeholder: '欢迎大佬指点~~~',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
